var chuanzhiid="";
//身份证
var patientCardId;

txkui.parse();
var rightmenushow = true;
$('.zdmenubuttom').click(function() {
    if (rightmenushow) {
        $('.rightmenu').animate({
            width: 'toggle'
        }, 350);
        rightmenushow = false;
        $('.zdmenubuttom').children("img").attr("src","../img/show.png")
    } else {
        $('.rightmenu').animate({
            width: 'toggle'
        }, 350);
        $('.zdmenubuttom').children("img").attr("src","../img/hide.png")
        rightmenushow = true;
    }
})
$(function(){

    if (window.top === window.self) { // are you trying to put self in an iframe?
        var getParameters = function (url) {
            var nameValues = [];
            if (url) {
                if (url.indexOf('?') == -1) {
                    return nameValues.push({ baseUrl: url });
                };
                var arrUrlParts = url.split('?');
                var baseUrl = arrUrlParts[0];
                var urlParameters=arrUrlParts[1].split('&');
                for (var i = 0; i < urlParameters.length; i++) {
                    var nameValue = urlParameters[i].split('=');
                    nameValues.push({ baseUrl: baseUrl, key: nameValue[0], urlvalue: nameValue[1]});
                };
                return nameValues;
            };
        };
        var sysid=getParameters(window.location.href)[0].urlvalue;
        $('.thisrightselect').val(sysid)
        loadrightlist(sysid);
    }else{
        //右侧随访登记加载
        $.ajax({
            type: "POST",
            dataType: "json",
            url:  SVR_PATH+"cqzd/do/diacinfo/queryDiacrisisInfo",
            data: {
                'key': '1',
                "isValid": '1'
            },
            success: function(result) {
                var ghlist = result;

                $('.xinruguahao').append($("#user").tmpl(ghlist));
            }
        });
        //右侧历史随访
        $.ajax({
            type: "POST",
            dataType: "json",
            url:  SVR_PATH+"cqzd/do/flupinfo/queryListByKey",
            data: {
                'key': '1'
            },
            success: function(result) {
                var ghlist = result;
                $('.lishiguahao').append($("#lishiuser").tmpl(ghlist));
            },
        });
    }
})

var binliFlupId="";

//右侧菜单点击
function rightuserclick(el) {
    $(".userclick").removeClass("userclick");
        $(el).addClass("userclick")
    $(".zhezhao").fadeOut(1000);
    var userid = $(el).children('.thisuserid').html();
    var username = $(el).find('.thisusername').html();
    patientCardId = $(el).find('.patientCardId').html();
    chuanzhiid="";
    var form = new mini.Form("#SFXXGLform");
    form.clear();
    var patient=mini.get("#patientName");
    var diacrisisId=mini.get("#diacrisisId");
    patient.setValue(username);
    diacrisisId.setValue(userid);
}
function rightlishiclick(el){
    $(".userclick").removeClass("userclick");
    $(el).addClass("userclick")
    $(".zhezhao").fadeOut(1000);
    var userid = $(el).children('.thisuserid').html();
    chuanzhiid=userid;
    binliFlupId=userid;
    $.ajax({
        type: "get",
        dataType: "json",
        url: SVR_PATH+"cqzd/do/flupinfo/get/" + userid,
        data: {},
        success: function(result) {
            patientCardId=result.data.flupAndDiacModel.patientCardId
            if (result.data.flupAndDiacModel.isSpecKeep === '1') {
                $('#specKeepId').css('display', 'block');
            } else{
                $('#specKeepId').css('display', 'none');
                mini.get("specKeepId").setValue("")
            }

            var form = new mini.Form("#SFXXGLform");
            form.setData(result.data);
        }
    });
}

//点击其他地方隐藏右侧
$(".bigdiv").click(function() {
    if (rightmenushow) {
        $('.rightmenu').animate({
            width: 'toggle'
        }, 350);
        rightmenushow = false;
    }
})

function submintSFXXGLform(){
    var loadingWin =mini.loading("请稍后...", "等待");

    if(mini.get('gestResultId').value=="1"){
        mini.get("childbirth").setRequired(true)
    }

    var form = new mini.Form("#SFXXGLform");
    form.validate();
    if (form.isValid() == false){
        mini.hideMessageBox(loadingWin);
		mini.alert("请完善您的数据");
		return;
	};
    var biglist = form.getData();
    biglist.flupAndDiacModel.flupId=chuanzhiid;
    biglist.flupAndDiacModel.isValid="1";
    var fileArray=document.querySelector('#thisnibaba');
    var formData = new FormData();
    formData.append("json",JSON.stringify(biglist));
    for(var i=0,f;f=fileArray.files[i];i++){
        formData.append("file", f);
    }
    if(chuanzhiid==""){
        $.ajax({
            url: SVR_PATH+"cqzd/do/flupinfo/addFlupInfo",
            data: formData,
            type: "POST",
            processData: false,
            contentType: false,
            success: function(data) {
                mini.hideMessageBox(loadingWin);
                if(data.errMsg){
                    mini.alert("新增失败,"+data.errMsg);
                    return;
                }
                mini.alert("新增成功")
                setTimeout(function () {
                    location.reload();
                },500);

            },
            error: function(e) {
                mini.hideMessageBox(loadingWin);
                mini.alert("新增失败")
            }
        });
    }else{
        $.ajax({
            url: SVR_PATH+"cqzd/do/flupinfo/updateFlupInfo",
            data: formData,
            type: "POST",
            processData: false,
            contentType: false,
            success: function(data) {
                mini.hideMessageBox(loadingWin);
                if(data.errMsg){
                    mini.alert("修改失败,"+data.errMsg);
                    return;
                }
                mini.alert("修改成功");
            },
            error: function(e) {
                mini.hideMessageBox(loadingWin);
                mini.alert("修改失败")
            }
        });
    }
}
function dropSFXXGLform(){
    var loadingWin =mini.loading("请稍后...", "等待");
    var form = new mini.Form("#SFXXGLform");
    var biglist = form.getData();
    biglist.flupAndDiacModel.flupId=chuanzhiid;
    biglist.flupAndDiacModel.isValid="2";
    var fileArray=document.querySelector('#thisnibaba');
    var formData = new FormData();
    formData.append("json",JSON.stringify(biglist));
    for(var i=0,f;f=fileArray.files[i];i++){
        formData.append("file", f);
    }

        $.ajax({
            url: SVR_PATH+"cqzd/do/flupinfo/updateFlupInfo",
            data: formData,
            type: "POST",
            processData: false,
            contentType: false,
            success: function(data) {
                mini.hideMessageBox(loadingWin);
                if(data.errMsg){
                    mini.alert("作废失败,"+data.errMsg);
                    return;
                }
                mini.alert("作废成功");
            },
            error: function(e) {
                mini.hideMessageBox(loadingWin);
                mini.alert("作废失败")
            }
        });
    }
function renewSFXXGLform(){
    var loadingWin =mini.loading("请稍后...", "等待");
    var form = new mini.Form("#SFXXGLform");
    var biglist = form.getData();
    biglist.flupAndDiacModel.flupId=chuanzhiid;
    biglist.flupAndDiacModel.isValid="1";
    var fileArray=document.querySelector('#thisnibaba');
    var formData = new FormData();
    formData.append("json",JSON.stringify(biglist));
    for(var i=0,f;f=fileArray.files[i];i++){
        formData.append("file", f);
    }

        $.ajax({
            url: SVR_PATH+"cqzd/do/flupinfo/updateFlupInfo",
            data: formData,
            type: "POST",
            processData: false,
            contentType: false,
            success: function(data) {
                mini.hideMessageBox(loadingWin);
                if(data.errMsg){
                    mini.alert("恢复失败,"+data.errMsg);
                    return;
                }
                mini.alert("恢复成功");
            },
            error: function(e) {
                mini.hideMessageBox(loadingWin);
                mini.alert("恢复失败")
            }
        });
}
$('.righttitle').click(function() {
    $(this).next().animate({
        height: 'toggle'
    }, 350);
})

function changeselect(){
    var selectkey=$(".thisrightselect").val();
    var selectvalid = $('.zuofeicheck').is(':checked');
    var valid = 1;
    if (selectvalid) {
        valid = null;
    }
    loadrightlist(selectkey,valid);
}

function loadrightlist(selectkey,isValid){
    //右侧随访登记加载
    $.ajax({
        type: "POST",
        dataType: "json",
        url:  SVR_PATH+"cqzd/do/diacinfo/queryDiacrisisInfo",
        data: {
            'key':selectkey,
            "isValid": isValid
        },
        success: function(result) {
            var ghlist = result;
            for(var i=0;i<ghlist.data.length;i++){
                if(ghlist.data[i].isValid==="2"){
                    ghlist.data[i].isValid="isValid"
                }else{
                    ghlist.data[i].isValid=""
                }
            }
            $('.xinruguahao').empty();
            $('.xinruguahao').append($("#user").tmpl(ghlist));
        }
    });
    //右侧历史随访
    $.ajax({
        type: "POST",
        dataType: "json",
        url:  SVR_PATH+"cqzd/do/flupinfo/queryListByKey",
        data: {
            'key': selectkey,
            "isValid": isValid
        },
        success: function(result) {
            var ghlist = result;
            for(var i=0;i<ghlist.data.length;i++){
                if(ghlist.data[i].isValid==="2"){
                    ghlist.data[i].isValid="isValid"
                }else{
                    ghlist.data[i].isValid=""
                }
            }
            $('.lishiguahao').empty();
            $('.lishiguahao').append($("#lishiuser").tmpl(ghlist));
        },
    });
}


mini.get('isSpecKeep').on("valuechanged", function(e) {
    if (this.getValue() === '1') {
        $('#specKeepId').css('display', 'block');
    } else{
        $('#specKeepId').css('display', 'none');
        mini.get("specKeepId").setValue("")
    }
});



//打开病例管理
function onclick4() {
    $(".tanchuang").css("display", "flex");
    loadwjxx(binliFlupId)
}
//关闭病例管理
function closeBLZL() {
    $(".tanchuang").css("display", "none");
}
//点击病例查询
function loadwjxx(binliFlupId) {
    $.ajax({
        type: "get",
        dataType: "json",
        url: SVR_PATH+"cqzd/do/flupfile/getFileByFkId/" + binliFlupId,
        data: {},
        success: function(result) {
            var files_grid = mini.get("files_grid");
            files_grid.setData(result.data)
        }
    });
}

//兵力管理里面的a标签
function onActionRenderer(e) {
    var row = e.row;
    var aa = row.fileName;
    var s = '<a href="'+SVR_PATH+'cqzd/do/flupfile/getFileByCode/' + row.fileCode + '">' + aa + '</a>';
    return s;

}


//导出
function exportThis() {
    window.open('/cqzd/FollowUp/selFollowUpAll?patientCardId='+patientCardId)
}